package com.woniu.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.exception.RebornException;
import com.woniu.exception.order.OrderException;
import com.woniu.exception.order.OrderExceptionCode;
import com.woniu.model.Result;
import com.woniu.order.client.DepotClient;
import com.woniu.order.client.FlowClient;
import com.woniu.order.client.UserClient;
import com.woniu.order.cliententity.entity.FlowHandle;
import com.woniu.order.cliententity.entity.TUser;
import com.woniu.order.cliententity.form.AddHandelForm;
import com.woniu.order.form.AddOrderDetailForm;
import com.woniu.order.form.Detail;
import com.woniu.order.mapper.OrderDetailsMapper;
import com.woniu.order.mapper.TOrderMapper;
import com.woniu.order.model.OrderDetails;
import com.woniu.order.model.TOrder;
import com.woniu.order.service.OrderDetailsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 吴彦祖
 * @since 2024年05月23日
 */
@Service
public class OrderDetailsServiceImpl extends ServiceImpl<OrderDetailsMapper, OrderDetails> implements OrderDetailsService {

    @Resource
    private OrderDetailsMapper orderDetailsMapper;

    @Resource
    private TOrderMapper orderMapper;

    @Resource
    private FlowClient flowClient;
    @Resource
    private DepotClient depotClient;
    @Resource
    private UserClient userClient;
    /**
     * 根据订单编号返回订单详情
     * @param orderId
     * @return
     */
    @Override
    public List<OrderDetails> selectAllOrderDetailByOrderId(String orderId) {
        QueryWrapper<OrderDetails> wrapper = new QueryWrapper<>();
        wrapper.eq("order_num",orderId);
        List<OrderDetails> list = orderDetailsMapper.selectList(wrapper);
        return list;
    }

    @Override
    public List<OrderDetails> employeeQuery(AddOrderDetailForm form, Integer userId,String orderNum,String image) {
        //判断订单是否存在
        TOrder tOrder = orderMapper.selectById(orderNum);
        if (tOrder==null){
            throw new OrderException(OrderExceptionCode.ORDER_EXCEPTION_CODE_NO_ORDER);
        }
        ArrayList<OrderDetails> orderDetails1 = new ArrayList<>();
        BigDecimal orderTotal = BigDecimal.valueOf(0);
        //查出用户信息
        Result<TUser> tUserResult = userClient.byAliPayUsername(tOrder.getUserId());
        if (tUserResult.getCode()!=200){
            throw new RebornException(tUserResult.getMessage(),tUserResult.getCode());
        }
        TUser user = tUserResult.getData();
        for (Detail form1 : form.getDetails()) {

            BigDecimal total = form1.getSinglePrice().multiply(BigDecimal.valueOf(form1.getNumber()));
            orderTotal = orderTotal.add(total);
            System.out.println(orderTotal+"====="+total);
            OrderDetails orderDetails = BeanUtil.toBean(form1, OrderDetails.class);
            //根据垃圾类型查询垃圾单位 用于详情表的新增
            Result<String> result = depotClient.getUnitById(Math.toIntExact(orderDetails.getJunkType()));
            if (result.getCode()!=200){
                throw new RebornException(result.getMessage(),result.getCode());
            }
            orderDetails.setId(Long.valueOf(tOrder.getId()));
            orderDetails.setAddress(tOrder.getAddress());
            orderDetails.setPhone(user.getPhone());
            orderDetails.setName(user.getUsername());
            orderDetails.setOrderNum(form.getOrderNum());
            orderDetails.setTotalPrice(total);
            orderDetails.setUnit(result.getData());
            orderDetails.setEmployId(userId.longValue());
            orderDetails.setCreateTime(LocalDateTime.now());
            orderDetailsMapper.insert(orderDetails);
            orderDetails1.add(orderDetails);
        }
        //修改订单状态
        AddHandelForm addHandelForm = new AddHandelForm(null, 5, null, orderNum, null);
        Result<FlowHandle> add = flowClient.add(addHandelForm);
        if (add.getCode()!=200){
            throw new OrderException(add.getCode(),add.getMessage());
        }
        tOrder.setImg(image);
        tOrder.setState((long)5);
        tOrder.setTotalPrice(orderTotal);
        orderMapper.updateById(tOrder);
        return orderDetails1;
    }


    }

